package git.bankview.wl.console;

import java.sql.Connection;
import java.sql.PreparedStatement;

import git.bankview.sl.db.DBConnectionFactory;
import laya.semi.process.IGenericClassActivity;
import laya.semi.process.IProcessContext;

import org.apache.log4j.Logger;

public class TaskDependenceSetter
	implements IGenericClassActivity
{
	private static Logger log = Logger.getLogger(TaskDependenceSetter.class);

	private static String SQL_DEPENDENCEINSERT = 
		"INSERT INTO taskdependence (datekey, taskid, deptaskid) VALUES (?, ?, ?)";
	
	public void execute(IProcessContext oCtx)
		throws Exception
	{
		java.sql.Date datekey = (java.sql.Date)oCtx.getField("datekey");
		Integer taskid = (Integer)oCtx.getField("taskid");
		Integer[] vDeptaskid = (Integer[])oCtx.getField("deptaskids");
		Integer[] vSubtaskid = (Integer[])oCtx.getField("subtaskids");
		
		Connection oConn = DBConnectionFactory.getCoreConnection();
		try
		{
			log.debug(SQL_DEPENDENCEINSERT);
			PreparedStatement ps = oConn.prepareStatement(SQL_DEPENDENCEINSERT);
			
			try
			{
				for (int i = 0; i < vDeptaskid.length; ++i)
				{
					ps.setDate(1, datekey);
					ps.setInt(2, taskid.intValue());
					ps.setInt(3, vDeptaskid[i].intValue());
					ps.addBatch();
				}
				
				for (int i = 0; i < vSubtaskid.length; ++i)
				{
					ps.setDate(1, datekey);
					ps.setInt(2, vSubtaskid[i].intValue());
					ps.setInt(3, taskid.intValue());
					ps.addBatch();
				}
				
				ps.executeBatch();
			}
			finally
			{
				ps.close();
			}
		}
		finally
		{
			oConn.close();
		}
	}
}
